
/*
 * GET home page.
 */

var mysql = require('../lib/db');
var date = require('../lib/date');
var exec = require("child_process").exec;
var fs = require('fs');
var URL  = require('url');
var db = new mysql.create();
exports.index = function(req, res){
	db.query('select * from sf_domain', function(err, r, field){
		if(err){
			res.render('index_nodata',{title: 'skipfish - no data',})
		}else{
			var domain = [];
			//get data information by domain
			var inString = [], data = [];
			for(var i=0; i<r.length; i++){
				var _domain = r[i].domain.replace('.','_').replace('.','_');
				if(!data[_domain]){
					inString.push("'" + r[i].domain + "'");
					data[_domain] = [];
					domain[i] = []
					domain[i].value = r[i].domain;
					domain[i].key = _domain;
				}
			}
			inString = inString.join();
			db.query("select * from sf_skipfish where `domain` in ("+inString+")",function(err,r,field){
				if(r){
					for(var i=0; i<r.length; i++){
						var _domain = r[i].domain.replace('.','_').replace('.', '_');
						data[_domain].push(r[i]);
					}
					res.render('index',{ title: 'Safe Dianping@F2E / home',domain: domain, data: data});
				}else{
					res.render('index',{title:'Safe Dianping@f2e',domain:false,data:false});
				}
			});
		}
	});
};
exports.crawl = function(req, res){
	var url = req.query;
	if(url.url){
		var db = new mysql.create();
		url['id'] = 0;
		db.query('select max(id) as id from sf_skipfish limit 1',function(err, r, fields){
			if(err){
				throw new Error('Faild to select max id from skipfish, the error:' + err);
			}
			if(r.id){
				url.id = r.id++;
			}else{
				url.id = 1;
			}
			if(!url.id){
				throw new Error('create url.id error');
			}
			getDirectoryId(url,res);
		});
	}else{
		res.redirect('/');
	}
}

function getDirectoryId(url,res){
	var id = url.id;
	fs.stat('/work/www/skipfish/'+id,function(err, stats){
		if(err){
			url.id = id;
			crawl(url,res);
		}else if(stats.isDirectory()){
			url.id++;
			return getDirectoryId.call(this, url, res);
		}
	});
}
function crawl(url,res){
	url = parseUrl(url);
	var query = "/work/soft/skipfish-2.05b/skipfish -W /dev/null -LY -P -o '/work/www/skipfish/'" + url.id + " " + url.url;
	exec(query, function(error, stdout, stderr){
		if(error){
			res.end('false');
			throw new Error('Error occuces by Exec skipfish' + error);
		}else{
			var db = new mysql.create();
			var path = createPath(url.id);
			var time = new Date().getTime();
			db.query("insert into sf_skipfish (`id`, `name`,`path`,`time`,`domain`,`url`) values (" + url.id + ",'" + url.url + "', '" + path + "', NOW(), '"+url.parmers.host+"', '"+url.url+"')",function(err, r, field){
				console.info('haha ,now insert db, success');
			});
			db.query("insert ignore into sf_domain (`domain`) values ('"+url.parmers.host+"')");
			res.end(''+path);
		}
	});
}

function parseUrl(url){
	var turl = URL.parse(url.url);
	if(!turl['protocol']){
		url.url = 'http://'+url.url;
		turl = URL.parse(url.url);
	}
	if(!turl['protocol'] && !turl['host']) return false;
	url.parmers = turl;
	return url;
}
function createPath(path){
	return path;
}
exports.item = function(req, res){
	console.info(req);
}
